home *** CD-ROM | disk | FTP | other *** search
- /* Copyright, 1990, Regents of the University of Colorado */
- /* This program computes a five point smoothing algorithm (each point is
- * set to be the average of his neighbors to the north, south, west, and east).
- * It operates on a matrix of size M x N, blocking the matrix into small
- * chuncks on a two dimensional processor array. M must be a multiple of P1,
- * and N a multiple of P2. */
-
- #include "dino.h"
-
- #define max(x,y) (x > y ? x : y)
- #define min(x,y) (x < y ? x : y)
-
- #define M 16
- #define N 16
- #define P1 4
- #define P2 4
-
- environment node[P1:id1][P2:id2] {
-
- composite smooth (a, in iter)
- double distributed a[M][N] map FivePt;
-
- /* ==> The FivePt mapping divides the matrix a
- into blocks of size M/P1 x N/P2. Each
- environment is assigned one of these
- blocks, plus the one element wide strips
- to each of the four sides. */
-
- int iter;
-
- {
- int i, j, k; /* Looping variables */
-
- int home_n, home_s, home_w, home_e;
- /* Boundaries of the home data, not including the edges of
- * the matrix. */
-
- int copy_n, copy_s, copy_w, copy_e;
- /* Boundaries of the copy data, not including the edges of
- * the matrix. */
-
- /* ==> To make the program easier to read and
- understand, varaibles containing
- the ranges of home and copy data on each
- processor are useful. */
-
- /* Compute home_n, home_s, home_w, and home_e */
- home_n = max (M/P1 * id1, 1);
- home_s = min ((id1 + 1) * M/P1 - 1, M-2);
- home_w = max (N/P2 * id2, 1);
- home_e = min ((id2 + 1) * N/P2 - 1, N-2);
-
- /* Compute copy_n, copy_s, copy_w, and copy_e */
- copy_n = max (home_n - 1, 1);
- copy_s = min (home_s + 1, M-2);
- copy_w = max (home_w - 1, 1);
- copy_e = min (home_e + 1, N-2);
-
- /* Repeat the smoothing process iter times */
- for (i = 0; i < iter; i++) {
-
- /* Send out your data and receive it back again, if not the first
- * iteration */
- if (i != 0) {
- a[<home_n,home_s>][<home_w,home_e>]# =
- a[<home_n,home_s>][<home_w,home_e>];
- a[<copy_n,copy_s>][<copy_w,copy_e>]#;
- }
-
- /* Perform the computation, but only on non-edge elements */
- for (j = home_n; j <= home_s; j++)
- for (k = home_w; k <= home_e; k++)
- a[j][k] = (a[j][k-1] + a[j][k+1] + a[j-1][k] + a[j+1][k]) / 4;
-
- }
- }
- }
-
- environment host {
-
- void main ()
-
- {
- double a[M][N]; /* Input data */
- int iter; /* Holds the iteration count */
-
- int i, j; /* Looping variables */
-
- /* Set up the initial data for a[][] */
- for (i = 0; i < M; i++)
- for (j = 0; j < N; j++)
- a[i][j] = (i + 1)*(j + 1);
- for (i = 1; i < M - 1; i++)
- for (j = 1; j < N - 1; j++)
- a[i][j] = 0;
-
- /* Set up the variable which will contain the number of iterations */
- iter = 500;
-
- /* Print out the initial data */
- printf ("Initial data for a:\n");
- for (i = 0; i < M; i++) {
- for (j = 0; j < N; j++)
- printf ("%7.2f", a[i][j]);
- printf ("\n");
- }
-
- /* Perform the computation */
- smooth (a[][], iter)#;
-
- /* Printout the results */
- printf ("Result data for a:\n");
- for (i = 0; i < M; i++) {
- for (j = 0; j < N; j++)
- printf ("%7.2f", a[i][j]);
- printf ("\n");
- }
- }
- }
-
-